package edu.siu.math.egut.main;

import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.Scanner;

import edu.siu.math.egut.io.Algorithms;
import edu.siu.math.egut.io.LiE;
import edu.siu.math.egut.io.OutputPref;
import edu.siu.math.egut.util.Group;
import edu.siu.math.egut.util.NonlinearException;
import edu.siu.math.egut.util.NotARootException;
import edu.siu.math.egut.util.Polynomial;
import edu.siu.math.egut.util.Unipotent;

public class SpEmbedding_WriteMathematicaSetup {

    private static final String INSTRUCTIONS = "Input is:\n " +
		"\t* a hand-typed list of roots for U/(U,U).  (The embedding being calculated\n" +
		"\t\tis sensitive to the order.)\n" +
		"\t* the string you would use to describe the unipotent radical to LiE\n " +
		"\t\t(e.g, \"1\" or \"[1,3]\"\n" +
		"\t* the name of the group.";

    /**
     * @param args
     */
    public static void main(String[] args) {
	// Extract input from file, after getting corrected filename as needed.  
	String[] input;
	try {
	    input = Algorithms.getCommandsFromFile(args[0]);
	} catch (FileNotFoundException e1) {
	    System.out.println("File not found.  Please try again.");
	    try {
		input = Algorithms.getCommandsFromFile(Algorithms.getFileName(new Scanner(System.in)));
	    } catch (FileNotFoundException e) {
		
		input=new String[1];
		System.out.println("File not found.");
		e.printStackTrace();
		System.exit(1);
	    }
	}
	
	// Parse input  
	if(input.length !=3){
	    System.out.println("This program needs 3 pieces of input."+INSTRUCTIONS);
	    System.exit(1);
	}

	Group g = Group.getGroup(input[input.length-1]);
	if( g== null){
	    System.out.println("Last argument should be the name of a simple exceptional group;\n \""+input[input.length-1]+"\" is not.\n"+INSTRUCTIONS);
	    System.exit(1);
	}
	LiE lie = new LiE();
	
	    
		int[][] uMat;
		try {
		    uMat = Algorithms.readLiEMatrix(lie.exec("unipRad("+input[1]+","+input[2]+")", OutputPref.HIDE));
		int[][] uPiece1 =Algorithms.readHandTyped(input[0]);
		
	
		int[][] leviRoots = Algorithms.readLiEMatrix(lie.exec("levi("+input[1]+","+input[2]+")^-levi("+input[1]+","+input[2]+")", OutputPref.HIDE));

	// Define x[alpha, r] for each root alpha 
		for( int[] a: leviRoots){
		    SpEmbedding.sunipotentEmbedding(OutputStyle.MATHEMATICA, input, g, uMat, uPiece1, a, Polynomial.create("r"));
		}
	
	// Define h[t1,t2,t3,t4]  
		// first need our list of roots to be a LiE-readable list.
		DPR_WriteMathematicaSetup.writeTorusDefinition(Algorithms.writeLieMatrix(uPiece1), g);
	
	// Define w[i] for each i.  This is exactly the same as in DPR_WMS, so we can just
		// call the method built there.
		DPR_WriteMathematicaSetup.writeWeylElementDefinitions(g);
	    
		} catch (IOException e) {
		    // TODO Auto-generated catch block
		    e.printStackTrace();
		} catch (NumberFormatException e) {
		    // TODO Auto-generated catch block
		    e.printStackTrace();
		} catch (NotARootException e) {
		    // TODO Auto-generated catch block
		    e.printStackTrace();
		} catch (NonlinearException e) {
		    // TODO Auto-generated catch block
		    e.printStackTrace();
		}

	

    }

}
